/**
 * Created by c on 2017-11-18.
 */
$(document).ready(function () {
    $('#proNum').select2({
        placeholder: "请选择",
        language: "zh-CN"
    });
    $('#woJubnum').select2({
        placeholder: "请选择",
        language: "zh-CN"
    });

    //初始化下拉列表
    initSelect();
    //获取最新采购编号
    getNumPurNum();
    //设置table背景颜色
    $("table#delTable").children().find("#back").css({"background-color":"#838383","color":"white"})

});


function initAddDrug(){
    $("div#addDrug").children().remove();
    $("div#updateDrug").children().remove();
    $("div.myForm form").clone().appendTo("#addDrug");
    $("div#addDrug select").attr("id","addDelSel");
    $('#addDelSel').select2();
    $("form p span").children().remove();
    //加载模态框
    $("#addDrug").dialog({
        title:"采购明细信息登记",
        btnValue:"添加",
        addBind:function(){
            if(!checkInput()){
                return;
            }
            addTd();
        }
    });
}


function initUpdateDrug(tr){
    $("div#addDrug").children().remove();
    $("div#updateDrug").children().remove();
    $("div.myForm form").clone().appendTo("#updateDrug");
    $("#updateDrug form select").remove();
    var $a = $("#updateDrug form").children(":eq(1)").children(":eq(1)");
    //把select替换成input
    $('<input type="text" name="drugName" readonly="readonly" class="form-control" value=""/>').appendTo($a);
    //添加隐藏于放置map的key
    $('<input type="hidden" name="mapKey" value=""/>').appendTo("#updateDrug form");
    $("div#updateDrug select").attr("id","upDelSel");
   /* $('#upDelSel').select2();*/
    $("form p span").children().remove();
    //加载模态框
    $("#updateDrug").dialog({
        title:"采购明细信息修改",
        btnValue:"修改",
        addBind:function(){
            if(!checkInput()){
                return;
            }
            var param = $("#updateDrug form").serialize();
            $.post(ctx+"/pur/updateDetails",param,function(purdetails){
                if(purdetails!=null){
                    toastr.success("操作提示","修改明细信息成功!");
                } else{
                    toastr.error("操作提示","系统繁忙,修改失败!");
                }
                var $td = $(tr).parent().parent();
                var data = eval("("+purdetails+")");
                $td.find(".delBuyprice1").html(data.delBuyprice);
                $td.find(".delRetailprice1").html(data.delRetailprice);
                $td.find(".delOneprice1").html(data.delOneprice);
                $td.find(".delBuynum1").html(data.delBuynum);
                $("#updateDrug").dialog("close");
            },"text");
        }
    });
}


//添加table子节点
function addTd(){
    var param = $("#addDrug form#purDelForm").serialize();
    $.post(ctx+"/pur/addDetails",param,function(newId){
        var mapKey = newId;
        //添加节点信息
        var drugName = $("#addDrug select[name='drugNum']").find("option:selected").text();
        var delBuyprice = $("#addDrug input[name='delBuyprice']").val();
        var delRetailprice = $("#addDrug input[name='delRetailprice']").val();
        var delOneprice = $("#addDrug input[name='delOneprice']").val();
        var delBuynum = $("#addDrug input[name='delBuynum']").val();
        var delQoh1 = $("#addDrug input[name='delQoh']").val();

        $("#addDrug").dialog("close");
        var $td = "<tr class='delsTr'>"+
            "<td class='drugName1'>"+
                "<span class='dName'>"+drugName+"</span>"+
                "<input type='hidden' name='delQoh1' value='"+delQoh1+"'/>"+
                "<input type='hidden' name='keyId1' value='"+mapKey+"'/>"+
            "</td>"+
            "<td class='delBuyprice1'>"+delBuyprice+"</td>"+
            "<td class='delRetailprice1'>"+delRetailprice+"</td>"+
            "<td class='delOneprice1'>"+delOneprice+"</td>"+
            "<td class='delBuynum1'>"+delBuynum+"</td>"+
            "<td >"+
            '<button class="layui-btn layui-btn-normal layui-btn-mini updateDrug" type="button" onclick="updateDetails(this)">编辑</button>' +
            '<button type="button" onclick="delDels(this)"  class="layui-btn layui-btn-danger layui-btn-mini del">删除</button>'
        "</td>"+
        "</tr>";
        $("#userAdd").before($td);

    },"text");
}
//初始化下拉列表
function initSelect(){
    $("#proNum").children(":gt(0)").remove();
    $("#woJubnum").children(":gt(0)").remove();
    $.post(ctx+"/pur/getSelect",null,function(map){
        $(map.pro).each(function(){
            console.log(this.key)
            var $opt = $("<option/>");
            $opt.val(this.proNum);
            $opt.html(this.proName);
            $opt.appendTo("#proNum");
        });

        $(map.worker).each(function(){
            var $opt = $("<option/>");
            $opt.val(this.woJubnum);
            $opt.html(this.woName);
            $opt.appendTo("#woJubnum");
        });
    },"json");
}

//获取最新的采购编号
function getNumPurNum(){
    $.post(ctx+"/pur/searchPurNum",null,function(purNum){
        $("input[name='planNum']").val(purNum);
    },"text");
}

//用户选择药品时ajax获取该药品的库存
function searchDrugStock(sel){
    var num = $(sel).val();
    if(num=="-1"){
        $("input[name='delQoh']").val("");
        return;
    }

    $.post(ctx+"/pur/getDrugStock","drugNum="+num,function(stock){
        $("input[name='delQoh']").val(stock);
    },"text");
}

//搜索日期插件
$(function () {
    laydate.render({
        elem:'[name=planDate]'
    })
});

//初始化药品下拉列表
function initDrugSel(){
    $(".jiaDrugNum").children().not(".bus").remove();
    $.ajax({
        url:ctx+"/pur/searchDrug",
        dataType:"json",
        async:false,
        success:function(drug){
            $(drug).each(function(){
                var $opt = $("<option/>");
                $opt.val(this.drugNum);
                $opt.html(this.drugName);
                $opt.appendTo(".jiaDrugNum");
            });
        }
    });
}

function dengji(){
    $("span.zhuErr").html("");
    //加载模态框信息
    initAddDrug();
    $(".modal-body form")[0].reset();
    $("#addDrug").dialog("open");
    //加载药品信息
    initDrugSel();
}

function updateDetails(tr){
    //加载模态框信息
    initUpdateDrug(tr);
    $(".modal-body form")[0].reset();
    $("#updateDrug").dialog("open");
    var $td = $(tr).parent().parent();
    var $form = $("#updateDrug form");
    //绑定修改信息
    $form.find("[name=mapKey]").attr("value",$td.find("[name='keyId1']").val());
    $form.children().find("[name=drugName]").attr("value",$td.find(".dName").html());
    $form.children().find("[name=delQoh]").val($td.find("[name='delQoh1']").val());
    $form.children().find("[name=delBuyprice]").val( $td.find(".delBuyprice1").html());
    $form.children().find("[name=delRetailprice]").val($td.find(".delRetailprice1").html());
    $form.children().find("[name=delOneprice]").val($td.find(".delOneprice1").html());
    $form.children().find("[name=delBuynum]").val($td.find(".delBuynum1").html());
}

function delDels(tr){
    var $td = $(tr).parent().parent();
    var key = $td.find("[name='keyId1']").val();

    swal({
        title:"确认提示",
        text:"确认移除此条信息?",
        type:"warning",
        showCancelButton:true,
        closeOnConfirm:false,
        confirmButtonText:"删除",
        confirmButtonColor:"#DD6B55",
        cancelButtonText: "取消"
    }).then(function(){
        $.post(ctx+"/pur/delDels","key="+key,function(flag){
            if(flag=="true"){
                toastr.success("操作提示","移除成功!");
                $td.remove();
            } else {
                toastr.error("操作提示","系统繁忙,移除失败!");
            }
        },"text");
    });
}

//提交审核
function subPurZhu(){
    if($("select[name='proNum']").val()=="-1"){
        $("span.zhuErr").html("请选择供应商(药商)待上传!");
        return;
    }

    if($("select[name='woJubnum']").val()=="-1"){
        $("span.zhuErr").html("请选择负责本次采购的采购员!");
        return;
    }

    var date = $("input[name='planDate']").val();
    var newDate = new Date(date);
    var sysDate=new Date();

    if(date=="" || date==null){
        $("span.zhuErr").html("请输入或选择计划采购日期!");
        return;
    }
    if(newDate<sysDate){
        $("span.zhuErr").html("计划采购日期必须大于今天!");
        return;
    }

    if($("#delTable .delsTr").html()==null){
        $("span.zhuErr").html("请录入采购明细信息待上传!");
        return;
    }


    var param = $("form.submit").serialize();
    var planNum = $("form.submit input[name=planNum]").val();
    $.post(ctx+"/pur/addPurplan",param,function(flag){
        if(flag=="true"){
            swal({
                title:"操作提示",
                text:"提交成功,请留意审核!",
                type:"success",
            }).then(function(){
                location.href=ctx+"/pur/purview/index?planNum="+planNum;
            })
        } else {
            toastr.success("系统繁忙,提交失败!");
        }
    },"text");
}

function checkInput(){
    if(!checkSel()||!checkdelBuyprice()||!checkdelRetailprice()||!checkdelOneprice()||!checkdelBuynum()){
        return false;
    }
    return true;
}

function checkSel(){
    var flag = "true";
    var num = $("#addDrug select[name='drugNum']").val();
    var drugName = $("#addDrug select[name='drugNum']").find("option:selected").text();
    if(num == "-1"){
        flag="false";
        $("span.checkSel").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请选择药品！</i>');
        return false;
    }

    $("#delTable tr").each(function(){
        if(drugName==$(this).children().find("span.dName").html()){
            flag="false";
            $("span.checkSel").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>此药品已选择</i>');
            return false;
        }
    })

    if(flag=="true"){
        $("span.checkSel").html('<i class="layui-icon" style="color:green;"></i>');
        return true;
    }
}

function checkdelBuyprice(){
    var delBuyprice = $("input[name='delBuyprice']").val();
    $("span.checkdelBuyprice").html(" ").css("color","red");
    if(delBuyprice == ""){
        $("span.checkdelBuyprice").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请录入购进价格！</i>');
        return false;
    } else if(isNaN(delBuyprice)==true){
        $("span.checkdelBuyprice").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请录入合法的参数！</i>');
        return false;
    } else if(parseFloat(delBuyprice)<=0){
        $("span.checkdelBuyprice").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>购进价格不能小于0！</i>');
        return false;
    } else {
        $("span.checkdelBuyprice").html('<i class="layui-icon" style="color:green;"></i>');
    }
    return true;
}


function checkdelRetailprice(){
    var delRetailprice = $("input[name='delRetailprice']").val();
    $("span.checkdelRetailprice").html(" ").css("color","red");
    if(delRetailprice == ""){
        $("span.checkdelRetailprice").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请录入批发价格！</i>');
        return false;
    } else if(isNaN(delRetailprice)==true){
        $("span.checkdelRetailprice").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请录入合法的参数！</i>');
        return false;
    } else if(parseFloat(delRetailprice)<=0){
        $("span.checkdelRetailprice").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>批发价格不能小于0！</i>');
        return false;
    } else {
        $("span.checkdelRetailprice").html('<i class="layui-icon" style="color:green;"></i>');
    }
    return true;
}

function checkdelOneprice(){
    var delOneprice = $("input[name='delOneprice']").val();
    $("span.checkdelOneprice").html(" ").css("color","red");
    if(delOneprice == ""){
        $("span.checkdelOneprice").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请录入零售价格！</i>');
        return false;
    } else if(isNaN(delOneprice)==true){
        $("span.checkdelOneprice").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请录入合法的参数！</i>');
        return false;
    } else if(parseFloat(delOneprice)<=0){
        $("span.checkdelOneprice").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>零售价格不能小于0！</i>');
        return false;
    } else {
        $("span.checkdelOneprice").html('<i class="layui-icon" style="color:green;"></i>');
    }
    return true;
}

function checkdelBuynum(){
    var delBuynum = $("input[name='delBuynum']").val();
    $("span.checkdelBuynum").html(" ").css("color","red");
    if(delBuynum == ""){
        $("span.checkdelBuynum").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请录入购进数量！</i>');
        return false;
    } else if(isNaN(delBuynum)==true){
        $("span.checkdelBuynum").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请录入合法的参数！</i>');
        return false;
    } else if(parseFloat(delBuynum)<=0){
        $("span.checkdelBuynum").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>购进数量不能小于0！</i>');
        return false;
    } else {
        $("span.checkdelBuynum").html('<i class="layui-icon" style="color:green;"></i>');
    }
    return true;
}